package com.chenfan.mcn.controller;

import com.chenfan.mcn.enums.AeQuarterlyTypeEnum;
import com.chenfan.mcn.extension.Res;
import com.chenfan.mcn.model.AeQuarterlyTable;
import com.chenfan.mcn.model.McnPage;
import com.chenfan.mcn.model.dto.AeQuarterlyTableDTO;
import com.chenfan.mcn.model.dto.AeQuarterlyTableSearchDTO;
import com.chenfan.mcn.model.dto.AeQuarterlyTableUpdateDTO;
import com.chenfan.mcn.model.dto.AeRoyaltiesExportDTO;
import com.chenfan.mcn.model.vo.AeQuarterlyBoxList;
import com.chenfan.mcn.service.AeQuarterlyTableService;
import com.chenfan.mcn.model.common.PageInfo;
import com.chenfan.privilege.common.config.SearchAuthority;
import com.chenfan.privilege.common.config.UrlAuth;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * AE季度单
 *
 * @Author chenguopeng
 * @Date 2021/6/7 15:44
 */
@Slf4j
@RestController
@RequestMapping("/aQuarterlyTable")
public class AeQuarterlyTableController {

    @Autowired
    private AeQuarterlyTableService aeQuarterlyTableService;

    /**
     * 列表查询
     *
     * @param dto
     * @return
     */
    @PostMapping("/v1/page")
    @UrlAuth({"aeCommission_search"})
    public Res<PageInfo<AeQuarterlyTable>> getPage(McnPage<AeQuarterlyTable> page, @SearchAuthority(filterCustomer = false) AeQuarterlyTableSearchDTO dto) {
        return Res.ok(aeQuarterlyTableService.getPage(page, dto));
    }

    /**
     * 季度提成单创建
     *
     * @param aeQuarterlyTableDTO
     * @return
     */
    @PostMapping("/v1/createOrUpdate")
    public Res<Object> createOrUpdate(@RequestBody AeQuarterlyTableDTO aeQuarterlyTableDTO) {
        Long id = aeQuarterlyTableService.createOrUpdate(aeQuarterlyTableDTO);
        return Res.ok(id);
    }

    /**
     * 季度提成单修改
     * v2.1版本兼容为保存并发放
     *
     * @param aeQuarterlyTableUpdateDTO
     * @return
     */
    @PostMapping("/v1/update")
    public Res<Object> update(@RequestBody AeQuarterlyTableUpdateDTO aeQuarterlyTableUpdateDTO) {
        aeQuarterlyTableService.update(aeQuarterlyTableUpdateDTO);
        if (Objects.nonNull(aeQuarterlyTableUpdateDTO.getStatus()) &&aeQuarterlyTableUpdateDTO.getStatus().equals(AeQuarterlyTypeEnum.YFF_CONTRACT.getCode())) {
            AeRoyaltiesExportDTO changeStatusParam = new AeRoyaltiesExportDTO();
            changeStatusParam.setAeQuarterlyId(aeQuarterlyTableUpdateDTO.getId());
            changeStatusParam.setStatus(aeQuarterlyTableUpdateDTO.getStatus());
            aeQuarterlyTableService.changeStatus(changeStatusParam);
        }
        return Res.ok();
    }

    /**
     * 修改状态
     *
     * @param aeRoyaltiesExportDTO
     * @return
     */
    @PostMapping("/v1/change/status")
    @UrlAuth({"aeCommission_confirmationProtection","aeCommission_cancellation"})
    public Res<Object> changeStatus(@RequestBody AeRoyaltiesExportDTO aeRoyaltiesExportDTO) {
        aeQuarterlyTableService.changeStatus(aeRoyaltiesExportDTO);
        return Res.ok();
    }

    /**
     * 获取季度提成单中的季度编号
     *
     * @return
     */
    @PostMapping("/v1/getAeQuarterlyCode")
    public Res<Object> getAeQuarterlyCode() {
        return Res.ok(aeQuarterlyTableService.getAeQuarterlyCode());
    }

    /**
     * 获取当前季度前六个月份
     *
     * @return {@link List<String>}
     */
    @GetMapping("/getMonths")
    public Res<List<String>> getMonths() {
        return Res.ok(aeQuarterlyTableService.getMonths());
    }


    /**
     * AE提成单新建页下拉筛选
     *
     * @return {@link AeQuarterlyBoxList}
     */
    @GetMapping("/getAeQuarterlyBoxList")
    public Res<AeQuarterlyBoxList> getAeQuarterlyBoxList() {
        return Res.ok(aeQuarterlyTableService.getAeQuarterlyBoxList());
    }

}
